package com.xiezc.fetch.spider.biquge;

import com.xiezc.common.entity.BookInfo;
import com.xiezc.common.entity.Chapter;
import com.xiezc.common.entity.ChapterText;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.time.LocalDateTime;
import java.util.List;
import java.util.regex.Pattern;

public class BiqugeChapterProcess implements PageProcessor {


    private Site site = Site.me()
            .setRetryTimes(3)
            .setSleepTime(100)
            .setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");

    Pattern compile1 = Pattern.compile("/wapbook/\\d+.html");
    Pattern compile = Pattern.compile("/\\d+_\\d+_\\d+_1");


    @Override
    public void process(Page page) {

        int statusCode = page.getStatusCode();
        if (statusCode != 200) {
            return;
        }

        Request request = page.getRequest();
        String url = request.getUrl();
        Html html = page.getHtml();
        //章节内容页
        Object chapter1 = request.getExtra("chapter");
        if (chapter1 != null) {
            Chapter chapterR = (Chapter) chapter1;
            Selectable cont = html.xpath("//div[@id='novelcontent']");
            String content = cont.xpath("//p[1]/text()").toString();
            chapterR.setSize(content.length());
            ChapterText chapterText = new ChapterText();
            chapterText.setContent(content);
            chapterText.setCreateTime(LocalDateTime.now());
            chapterText.setFetchUrl(url);
            page.putField("chapterText", chapterText);
            page.putField("chapter", chapterR);
        }


        //目录页
        if (compile.matcher(url).find()) {

            BookInfo bookInfo = (BookInfo) request.getExtra("bookInfo");
            //获得每一章节的连接
            List<Selectable> nodes = html.xpath("//ul[@class='chapters']/li").nodes();
            nodes.forEach(node -> {
                Chapter chapter = new Chapter();
                chapter.setBookId(bookInfo.getId());
                String href = node.links().get();

                chapter.setFetchUrl(href);

                String chapterName = node.xpath("//a/text()").get();
                chapter.setName(chapterName);

                chapter.setCreateTime(LocalDateTime.now());
                Request request1 = new Request(href);
                request1.putExtra("chapter", chapter);
                page.addTargetRequest(request1);
            });
            //下一页与其他页目录
            List<String> all = html.xpath("//div[@class='page']").links().all();
            all.forEach(urlx -> {
                Request request1 = new Request(urlx);
                request1.putExtra("bookInfo", bookInfo);
                page.addTargetRequest(request1);
            });
        }


        //小说介绍详情页
        if (compile1.matcher(url).find()) {

            BookInfo bookInfo=(BookInfo)request.getExtra("bookInfo");
            if(bookInfo==null){
                return;
            }
            //封面链接
            Selectable info = html.xpath("//div[@class='infohead']");

            String chapterLinks = info.xpath("//ul[@class='infolink']/li[2]").links().get();

            //进入目录页带上小说信息
            String urlc = chapterLinks.substring(0, chapterLinks.length() - 1) + "_1/";
            Request requestC = new Request(urlc);
            requestC.putExtra("bookInfo", bookInfo);
            page.addTargetRequest(requestC);
            return;
        }


    }

    @Override
    public Site getSite() {
        return site;
    }

}
